﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class SizeBLL
  {
    public List<Size> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Sizes.OrderBy(p => p.Sort).ToList();
      }
    }
    public List<Size> GetByMode(int mode)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Sizes.Where(p => p.Mode == mode).OrderBy(p => p.Sort).ToList();
      }
    }
    public Size GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Sizes.Where(p => p.Id == id).FirstOrDefault();
      }
    }
    public List<Size> GetByProductId(string productId)
    {
      ProductInSizeBLL data = new ProductInSizeBLL();
      List<Size> listOut = new List<Size>();

      var list = data.GetByProductId(productId);
      foreach (var item in list)
      {
        Size obj = GetById(item.IdSize);
        if (obj != null)
        {
          listOut.Add(obj);
        }
      }
      return listOut;
    }
    //Get max Sort

    public byte GetMaxSort(int mode)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Size obj = data.Sizes.Where(p => p.Mode == mode).OrderByDescending(p => p.Sort).FirstOrDefault();

        if (obj == null) return 1;
        else return (byte)(obj.Sort + 1);
      }
    }

    //Swap sort

    public void SwapSort(int idFirst, int idSecond)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Size objFirst = data.Sizes.Where(p => p.Id == idFirst).FirstOrDefault();
        Size objSecond = data.Sizes.Where(p => p.Id == idSecond).FirstOrDefault();

        int tg = objFirst.Sort;
        objFirst.Sort = objSecond.Sort;
        objSecond.Sort = tg;

        data.SubmitChanges();
      }
    }
    public void Add(Size obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Sizes.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }
    public void Update(Size size)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Size obj = data.Sizes.Where(p => p.Id == size.Id).FirstOrDefault();
        if (obj != null)
        {
          obj.Name = size.Name;
          obj.Mode = size.Mode;
          obj.Sort = size.Sort;
        }
        data.SubmitChanges();
      }
    }
    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Size obj = data.Sizes.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
        {
          data.Sizes.DeleteOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }
    public void DeleteByMode(int mode)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        List<Size> obj = data.Sizes.Where(p => p.Mode == mode).ToList();
        if (obj != null)
        {
          data.Sizes.DeleteAllOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }
  }
}
